package kuzmenkov.oip.adapters.sun.jai;

import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.operator.MedianFilterDescriptor;
import javax.media.jai.operator.MedianFilterShape;
import javax.media.jai.operator.FileStoreDescriptor;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.util.logging.Logger;

/**
 * The class adapts JAT Median Filtering
 * User: Michael Kuzmenkov
 * Date: 26.03.2009
 * Time: 1:32:53
 */
public class MedianFilteringAdapter extends  ImageProcessingAdapter {
    private static final Logger log = Logger.getLogger(MedianFilteringAdapter.class.getName());

 
    /**
     * It takes three parameters. First parameter is the path to the image (String).
     * Second parameter is the shape of the mask (Integer). Third parameter is
     * the size (width and height) of the mask (Integer).
     * It returns one result. This is the output image (javax.media.jai.RenderedOp)
     * <p/>
     * The mask shape can be:
     * 0 (MEDIAN_MASK_SQUARE) - A square-shaped mask. The default.
     * 1 (MEDIAN_MASK_PLUS) - A plus-shaped mask.
     * 2 (MEDIAN_MASK_X) - An X-shaped mask.
     * 3 (MEDIAN_MASK_SQUARE_SEPARABLE) - A separable square mask, used for the separable median operation
     *
     * @param params the input parameters
     * @return the output image
     */
    public Object[] process(Object[] params) throws Exception {
        // Load the source image from a file.
        String path = (String) params[0];
        Integer maskShape = (Integer) params[1];
        Integer maskSize = (Integer) params[2];
        log.info("path=" + path + ", maskShape=" + maskShape + ", maskSize=" + maskSize);

        FileTool ftool = FileTool.getInstance();
        path = ftool.relPathToAbs(path);
        log.info("modified path="+path);
        RenderedOp srcImage = JAI.create("fileload", path);
        log.info("srcImage=" + srcImage);

        MedianFilterShape shape;
        int ms = maskShape;
        switch (ms) {
            case 0:
                shape = MedianFilterDescriptor.MEDIAN_MASK_SQUARE;
                break;
            case 1:
                shape = MedianFilterDescriptor.MEDIAN_MASK_PLUS;
                break;
            case 2:
                shape = MedianFilterDescriptor.MEDIAN_MASK_X;
                break;
            case 3:
                shape = MedianFilterDescriptor.MEDIAN_MASK_SQUARE_SEPARABLE;
                break;
            default:
                log.warning("The mask shape is wrong. The square mask will be used");
                shape = MedianFilterDescriptor.MEDIAN_MASK_SQUARE;


        }
        ParameterBlock block = new ParameterBlock();
        block.addSource(srcImage);
        block.add(shape);
        block.add(maskSize);
        RenderedOp dstImage = JAI.create("medianfilter", block, null);

        String dstFileName = "1.png";
        String dstPath = ftool.getTempDir(getSessionId())+dstFileName;
        log.info("destination path="+dstPath);
        JAI.create("filestore", dstImage, dstPath, "PNG", null);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        log.info("relative destination path="+relDstPath);
        return new Object[]{relDstPath};
    }
}
